﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;

namespace Eflong.Config
{
    /// <summary>
    /// 监控系统的参数
    /// </summary>
    public class KWSysParas
    {
        public KWSysParas( )
        {
            KJType = "KJ90N";

            KJPath = @"d:\data";

            DevFile = "dev.txt";

            RtDataFile = "rtdata.txt";

            DevFile2 = null;
            RtDataFile2 = null;
            KJParaFile = null;

            InitKJParas( );
        }

        private void InitKJParas( )
        {
            //设置传感器报警状态
            KJAlarmStates_dic = new Dictionary<string , bool>( );
            for ( int i = 0; i < KJDefaultStates.Length; i++ )
            {
                KJAlarmStates_dic.Add( KJStateNames[ i ] , KJDefaultStates[ i ] );
            }

            //开关量状态的定义字典
            KJDigitalStates_dic = new Dictionary<string , int>( );
            for ( int i = 0; i < KJDigitalTypes.Length; i++ )
            {
                KJDigitalStates_dic.Add( KJDigitalTypes[ i ] , KJDigitalDefaultNormalState );
            }

            // 模拟量报警的字典
            KJAnalogAlarms_dic = new Dictionary<string , bool>( );
            for ( int i = 0; i < KJAnalogAlarmTypes.Length; i++ )
            {
                KJAnalogAlarms_dic.Add( KJAnalogAlarmTypes[ i ] , KJAnalogDefaultAlarms[ i ] );
            }

            // 开关量报警的字典
            KJDigitalAlarms_dic = new Dictionary<string , bool>( );
            for ( int i = 0; i < KJDigitalAlarmTypes.Length; i++ )
            {
                KJDigitalAlarms_dic.Add( KJDigitalAlarmTypes[ i ] , KJDigitalDefaultAlarms[ i ] );
            }
        }

        /// <summary>
        /// 设置参数集合
        /// </summary>
        /// <param name="kvcc"></param>
        public void SetKJSysParas( KeyValueConfigurationCollection kvcc )
        {
            SetCommonKJParas( kvcc );

            SetAlarmStateParas( kvcc );

            SetAnalogAlarmParas( kvcc );

            SetDigitalAlarmParas( kvcc );

            SetDigitalStateParas( kvcc );
        }

        /// <summary>
        /// 获取参数集合
        /// </summary>
        /// <param name="nvc"></param>
        public void GetKJSysParas( NameValueCollection nvc )
        {
            GetCommonKJParas( nvc );

            GetAlarmStateParas( nvc );

            GetAnalogAlarmParas( nvc );

            GetDigitalAlarmParas( nvc );

            GetDigitalStateParas( nvc );

            ValideKJParas( );
        }

        /// <summary>
        /// 获取公共联网参数
        /// </summary>
        /// <param name="nvc"></param>
        private void GetCommonKJParas( NameValueCollection nvc )
        {
            string s;
            s = nvc[ "联网系统类型" ];
            if ( s != null )
            {
                KJType = s.ToUpper( );
            }

            s = nvc[ "联网系统路径" ];
            if ( s != null )
            {
                KJPath = s;
            }

            s = nvc[ "联网设备参数文件" ];
            if ( s != null )
            {
                DevFile = s;
            }

            s = nvc[ "联网实时数据文件" ];
            if ( s != null )
            {
                RtDataFile = s;
            }

            s = nvc[ "联网实时数据文件2" ];
            if ( s != null )
            {
                RtDataFile2 = s;
            }

            s = nvc[ "参数文件" ];
            if ( s != null )
            {
                KJParaFile = s;
            }
        }

        /// <summary>
        /// 设置公共联网参数
        /// </summary>
        /// <param name="nvc"></param>
        private void SetCommonKJParas( KeyValueConfigurationCollection kvcc )
        {
            try
            {
                kvcc[ "联网系统类型" ].Value = KJType;
            }
            catch ( Exception )
            {
                kvcc.Add( "联网系统类型" , KJType );
            }

            try
            {
                kvcc[ "联网系统路径" ].Value = KJPath;
            }
            catch ( Exception )
            {
                kvcc.Add( "联网系统路径" , KJPath );
            }

            try
            {
                kvcc[ "联网设备参数文件" ].Value = DevFile;
            }
            catch ( Exception )
            {
                kvcc.Add( "联网设备参数文件" , DevFile );
            }

            try
            {
                kvcc[ "联网实时数据文件" ].Value = RtDataFile;
            }
            catch ( Exception )
            {
                kvcc.Add( "联网实时数据文件" , RtDataFile );
            }

            if ( RtDataFile2 != null )
            {
                try
                {
                    kvcc[ "联网实时数据文件2" ].Value = RtDataFile2;
                }
                catch ( Exception )
                {
                    kvcc.Add( "联网实时数据文件2" , RtDataFile2 );
                }
            }

            if ( KJParaFile != null )
            {
                try
                {
                    kvcc[ "参数文件" ].Value = KJParaFile;
                }
                catch ( Exception )
                {
                    kvcc.Add( "参数文件" , KJParaFile );
                }
            }
        }

        /// <summary>
        /// 校验参数
        /// </summary>
        public void ValideKJParas( )
        {
            string devFile = string.Empty;
            string rtdataFile = string.Empty;
            switch ( KJType )
            {
                case "KJ66N":
                case "KJ70N":
                case "KJ75N":
                case "KJ76N":
                case "KJ86N":
                case "KJ90N":
                case "KJ95N":
                case "KJ92N":
                case "KJ126N":
                case "KJ103N":
                    devFile = "dev.txt";
                    rtdataFile = "rtdata.txt";
                    break;

                case "KJ4N":
                case "KJXY":
                    devFile = "InOutDefine";
                    if ( string.IsNullOrEmpty( rtdataFile ) )
                    {
                        rtdataFile = "实时数据;开关量";
                    }
                    break;

                case "KJ80N":
                case "KJ2000F":
                    devFile = "initdata.txt";
                    rtdataFile = "realdata.txt";
                    break;

                case "KJOPC":
                    devFile = "OpcDev.txt";
                    rtdataFile = "OpcRTData.txt";
                    break;

                case "KJQHWL":
                case "TDAUTO":
                    devFile = "dev.txt";
                    rtdataFile = "rtdata.txt";
                    break;
            }
            if ( DevFile == null || DevFile == "" )
            {
                DevFile = devFile;
            }
            if ( RtDataFile == null || RtDataFile == "" )
            {
                RtDataFile = rtdataFile;
            }
        }

        /// <summary>
        /// 监控系统类型名
        /// </summary>
        public string KJType { get; set; }

        /// <summary>
        /// 监控系统的文件路径
        /// </summary>
        public string KJPath { get; set; }

        /// <summary>
        /// 监控系统的测点定义文件
        /// </summary>
        public string DevFile { get; set; }

        public string DevFile2 { get; set; }

        /// <summary>
        /// 监控系统的实时数据文件
        /// </summary>
        public string RtDataFile { get; set; }

        public string RtDataFile2 { get; set; }

        /// <summary>
        /// 监控系统的参数定义文件
        /// </summary>
        public string KJParaFile { get; set; }

        /// <summary>
        /// 开关量传感器的状态是否与默认一致
        /// </summary>
        public Dictionary<string , int> KJDigitalStates_dic { get; set; }

        /// <summary>
        /// 模拟量报警的字典
        /// </summary>
        public Dictionary<string , bool> KJAnalogAlarms_dic { get; set; }

        /// <summary>
        /// 开关量报警的字典
        /// </summary>
        public Dictionary<string , bool> KJDigitalAlarms_dic { get; set; }

        public string[ ] KJNames = new string[ 13 ]
        {
            "KJ90N", "KJ95N", "KJ4N", "KJ66N", "KJ70N", "KJ75N",
            "KJ83N",  "KJ86N","KJ92N", "KJ100N", "KJ101N",  "KJ126N", "KJXYN",
        };

        /// <summary>
        /// 定义的常用开关量传感器的类型
        /// </summary>
        public string[ ] KJDigitalTypes = new string[ 12 ] {
           "风机", "风门" ,"风筒", "烟雾", "开停", "开关" ,
           "断电", "馈电","闭锁","控制","分站","供电",
        };

        /// <summary>
        /// 开关量默认的正常状态对应的数值
        /// KJ126 1开，0停、关
        /// </summary>
        public const int KJDigitalDefaultNormalState = 1;

        /// <summary>
        /// 定义的常用开关量传感器的正常名称集合
        /// </summary>
        public string[ ] KJDigitalNormalNames = new string[ 12 ]{
           "开", "关" ,"正常", "无烟", "开", "开" ,
           "正常", "有电","吸合","正常","正常","交流",
        };

        /// <summary>
        /// 定义的常用开关量传感器的异常名称集合
        /// </summary>
        public string[ ] KJDigitalAlarmNames = new string[ 12 ]{
           "停", "开" ,"漏风", "有烟", "停", "关" ,
           "断电", "无电","断开","异常","异常","直流",
        };

        /// <summary>
        /// 获取开关量状态定义参数
        /// </summary>
        /// <param name="nvc"></param>
        public void GetDigitalStateParas( NameValueCollection nvc )
        {
            string s = string.Empty;
            int n = 0;

            for ( int i = 0; i < KJDigitalTypes.Length; i++ )
            {
                string name = KJDigitalTypes[ i ];
                s = nvc[ name + "-正常状态" ];
                if ( s != null )
                {
                    if ( int.TryParse( s , out n ) )
                    {
                        KJDigitalStates_dic[ name ] = n;
                    }
                    else
                    {
                        KJDigitalStates_dic[ name ] = KJDigitalDefaultNormalState;
                    }
                }
                else
                {
                    KJDigitalStates_dic[ name ] = KJDigitalDefaultNormalState;
                }
            }
        }

        /// <summary>
        /// 设置开关量的状态对应的数值
        /// </summary>
        /// <param name="kvcc">KeyValueConfigurationCollection 配置文件的KeyValue 集合</param>
        public void SetDigitalStateParas( KeyValueConfigurationCollection kvcc )
        {
            foreach ( var key in KJDigitalStates_dic.Keys )
            {
                try
                {
                    kvcc[ key + "-正常状态" ].Value = KJDigitalStates_dic[ key ].ToString( );
                }
                catch ( Exception )
                {
                    kvcc.Add( key + "-正常状态" , KJDigitalStates_dic[ key ].ToString( ) );
                }
            }
        }

        /// <summary>
        /// 模拟量报警类型
        /// </summary>
        public string[ ] KJAnalogAlarmTypes = new string[ ] {
            "甲烷","一氧化碳","风速","温度","压力","全部模",
        };

        /// <summary>
        /// 模拟量报警默认设置
        /// </summary>
        public bool[ ] KJAnalogDefaultAlarms = new bool[ ] {
            true, true, false,false,false,false,
        };

        /// <summary>
        /// 获取 模拟量报警类型参数
        /// </summary>
        /// <param name="nvc"></param>
        private void GetAnalogAlarmParas( NameValueCollection nvc )
        {
            for ( int i = 0; i < KJAnalogDefaultAlarms.Length; i++ )
            {
                GetNameValueCollectionToDictionary( nvc , KJAnalogAlarmTypes[ i ] , KJAnalogDefaultAlarms[ i ] , KJAnalogAlarms_dic );
            }
        }

        private void SetAnalogAlarmParas( KeyValueConfigurationCollection kvcc )
        {
            SetKeyValueConfigurationCollectionFromDictionary( kvcc , KJAnalogAlarms_dic );
        }

        /// <summary>
        /// 开关量报警类型
        /// </summary>
        public string[ ] KJDigitalAlarmTypes = new string[ ] {
            "主扇","局扇","风门","断电", "烟雾","全部开"
        };

        /// <summary>
        /// 开关量报警默认设置
        /// </summary>
        public bool[ ] KJDigitalDefaultAlarms = new bool[ ] {
            true, true, false,false,true,false,
        };

        /// <summary>
        /// 获取开关量报警类型参数
        /// </summary>
        /// <param name="nvc"></param>
        private void GetDigitalAlarmParas( NameValueCollection nvc )
        {
            for ( int i = 0; i < KJDigitalDefaultAlarms.Length; i++ )
            {
                GetNameValueCollectionToDictionary( nvc , KJDigitalAlarmTypes[ i ] , KJDigitalDefaultAlarms[ i ] , KJDigitalAlarms_dic );
            }
        }

        private void SetDigitalAlarmParas( KeyValueConfigurationCollection kvcc )
        {
            SetKeyValueConfigurationCollectionFromDictionary( kvcc , KJDigitalAlarms_dic );
        }

        /// <summary>
        /// 报警状态的字典
        /// </summary>
        public Dictionary<string , bool> KJAlarmStates_dic { get; set; }

        /// <summary>
        /// 传感器报警状态的数组
        /// </summary>
        public string[ ] KJStateNames = new string[ ] {
            "超上限","超下限","超断电", "超量程", "调校","负漂","断线","通信中断","故障","未知",
        };

        /// <summary>
        /// 报警默认设置
        /// </summary>
        public bool[ ] KJDefaultStates = new bool[ ] {
            true,false, true,false, false,false,true,true,true,false,
        };

        /// <summary>
        /// 获取传感器状态报警参数
        /// </summary>
        /// <param name="nvc"></param>
        private void GetAlarmStateParas( NameValueCollection nvc )
        {
            for ( int i = 0; i < KJStateNames.Length; i++ )
            {
                GetNameValueCollectionToDictionary( nvc , KJStateNames[ i ] , KJDefaultStates[ i ] , KJAlarmStates_dic );
            }
        }

        /// <summary>
        /// 设置报警状态的参数到配置文件中
        /// </summary>
        /// <param name="kvcc"></param>
        private void SetAlarmStateParas( KeyValueConfigurationCollection kvcc )
        {
            SetKeyValueConfigurationCollectionFromDictionary( kvcc , KJAlarmStates_dic );
        }

        public void GetNameValueCollectionToDictionary( NameValueCollection nvc , string name , bool defaultValue , Dictionary<string , bool> dic )
        {
            if ( dic.ContainsKey( name ) == false ) return;

            string s;
            int b = 0;
            s = nvc[ name + "-报警" ]; // 1 报警， 0 不报警
            if ( s != null )
            {
                if ( int.TryParse( s , out b ) )
                {
                    dic[ name ] = ( b == 1 );
                }
                else
                {
                    dic[ name ] = defaultValue;
                }
            }
            else
            {
                dic[ name ] = defaultValue;
            }
        }

        public void SetKeyValueConfigurationCollectionFromDictionary( KeyValueConfigurationCollection kvcc , Dictionary<string , bool> dic )
        {
            foreach ( var key in dic.Keys )
            {
                try
                {
                    kvcc[ key + "-报警" ].Value = dic[ key ] ? "1" : "0";
                }
                catch ( Exception )
                {
                    kvcc.Add( key + "-报警" , dic[ key ] ? "1" : "0" );
                }
            }
        }
    }
}